package cn.itcast.service.impl;

import cn.itcast.dao.OrderSettingDao;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.pojo.OrderSettingVo;
import cn.itcast.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //查询封装对象
    @Override
    public List<OrderSettingVo> findpage(String date) {
        List<OrderSettingVo>list =new ArrayList<>();
            //查出ID数组
            Integer[]vo=  orderSettingDao.selectid(date);
        for (Integer integer : vo) {
            //根据数组查询对象并添加对象
            OrderSettingVo orderSettingVo=orderSettingDao.findpage(integer);
            list.add(orderSettingVo);
        }
        for (OrderSettingVo orderSetting : list) {
            //拿到时间里的日
            String[] split = orderSetting.getOrderDate().split("-");
            orderSetting.setOrderDate(split[split.length-1]);
        }
        return list;
    }

    @Override
    public void updateNumber(String orderDate, int number) {

        //修改可预约人数
        orderSettingDao.deleteNumber(orderDate);
        orderSettingDao.updateNumber(orderDate,number);

    }

    @Override
    public void postData(List<String[]> list) {
        for (String[] arr : list) {
            //查询是否已经存在可预约，存在就删除在添加
            Integer number=orderSettingDao.selectdate(arr[0]);
            System.out.println(number);
            if (number==0){
                //添加数据
                orderSettingDao.updateNumber(arr[0],Integer.parseInt(arr[1]));
            }else {
                //修改数据
                orderSettingDao.deleteNumber(arr[0]);
                orderSettingDao.updateNumber(arr[0],Integer.parseInt(arr[1]));
            }
        }

    }
}
